package org.lex.keytool.ui.task;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;

import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPasswordField;
import javax.swing.JSeparator;
import javax.swing.JTextField;

import org.lex.keytool.Swings;
import org.lex.keytool.core.service.KeyTools;
import org.lex.keytool.core.task.ExportCertificateTask;
import org.lex.keytool.ui.builder.FileChooserBuilder;

public class ExportCertificateTaskPane extends BaseTaskPane {
	public static void main(String[] args) {
		Swings.show(new ExportCertificateTaskPane());
	}

	private JButton inputButton;
	private JButton outputButton;
	private JCheckBox rfcCheckBox;
	private JComboBox storeTypeComboBox;
	private JLabel inputLabel;
	private JLabel aliasLabel;
	private JLabel passwordLabel;
	private JLabel storeTypeLabel;
	private JLabel outputLabel;
	private JTextField inputTextField;
	private JTextField aliasTextField;
	private JPasswordField passwordField;
	private JTextField outputTextField;

	public ExportCertificateTaskPane() {
		initComponents();
	}

	@Override
	public void run() {
		ExportCertificateTask task = new ExportCertificateTask();
		task.setAlias(this.aliasTextField.getText());
		task.setFile(this.outputTextField.getText());
		task.setStoreType(this.storeTypeComboBox.getSelectedItem().toString());
		task.setKeyStore(this.inputTextField.getText());
		task.setStorePassword(new String(this.passwordField.getPassword()));
		task.setRfc(this.rfcCheckBox.isSelected());

		this.runTask(task);
	}

	private void initComponents() {
		GridBagConstraints gc;

		setLayout(new GridBagLayout());

		inputLabel = new JLabel("Input");
		gc = new GridBagConstraints();
		gc.gridx = 0;
		gc.gridy = 0;
		gc.anchor = GridBagConstraints.BASELINE_LEADING;
		gc.insets = new Insets(0, 0, 5, 5);
		add(inputLabel, gc);

		inputTextField = new JTextField();
		gc = new GridBagConstraints();
		gc.gridx = 1;
		gc.gridy = 0;
		gc.fill = GridBagConstraints.HORIZONTAL;
		gc.anchor = GridBagConstraints.BASELINE_LEADING;
		gc.weightx = 10.0;
		gc.insets = new Insets(0, 0, 5, 5);
		add(inputTextField, gc);

		inputButton = new JButton("Open..");
		gc = new GridBagConstraints();
		gc.gridy = 0;
		gc.fill = GridBagConstraints.HORIZONTAL;
		gc.anchor = GridBagConstraints.BASELINE_LEADING;
		gc.insets = new Insets(0, 0, 5, 0);
		add(inputButton, gc);
		FileChooserBuilder.addHandler(inputButton, inputTextField, true);

		aliasLabel = new JLabel("Alias");
		gc = new GridBagConstraints();
		gc.gridx = 0;
		gc.gridy = 1;
		gc.anchor = GridBagConstraints.BASELINE_LEADING;
		gc.insets = new Insets(0, 0, 5, 5);
		add(aliasLabel, gc);

		aliasTextField = new JTextField("mykey");
		gc = new GridBagConstraints();
		gc.gridx = 1;
		gc.gridy = 1;
		gc.fill = GridBagConstraints.HORIZONTAL;
		gc.anchor = GridBagConstraints.BASELINE_LEADING;
		gc.weightx = 10.0;
		gc.insets = new Insets(0, 0, 5, 5);
		add(aliasTextField, gc);

		passwordLabel = new JLabel("Password");
		gc = new GridBagConstraints();
		gc.gridx = 0;
		gc.gridy = 2;
		gc.anchor = GridBagConstraints.BASELINE_LEADING;
		gc.insets = new Insets(0, 0, 5, 5);
		add(passwordLabel, gc);

		passwordField = new JPasswordField();
		gc = new GridBagConstraints();
		gc.gridx = 1;
		gc.gridy = 2;
		gc.fill = GridBagConstraints.HORIZONTAL;
		gc.anchor = GridBagConstraints.BASELINE_LEADING;
		gc.weightx = 10.0;
		gc.insets = new Insets(0, 0, 5, 5);
		add(passwordField, gc);

		storeTypeLabel = new JLabel("Store Type");
		gc = new GridBagConstraints();
		gc.gridx = 0;
		gc.gridy = 3;
		gc.anchor = GridBagConstraints.BASELINE_LEADING;
		gc.insets = new Insets(0, 0, 5, 5);
		add(storeTypeLabel, gc);

		storeTypeComboBox = new JComboBox(KeyTools.getSupportKeyStoreTypes());
		gc = new GridBagConstraints();
		gc.gridx = 1;
		gc.gridy = 3;
		gc.fill = GridBagConstraints.HORIZONTAL;
		gc.anchor = GridBagConstraints.BASELINE_LEADING;
		gc.weightx = 10.0;
		gc.insets = new Insets(0, 0, 5, 5);
		add(storeTypeComboBox, gc);

		gc = new GridBagConstraints();
		gc.gridx = 0;
		gc.gridy = 4;
		gc.gridwidth = 3;
		gc.weightx = 10;
		gc.anchor = GridBagConstraints.BASELINE_LEADING;
		gc.fill = GridBagConstraints.HORIZONTAL;
		gc.insets = new Insets(0, 0, 5, 0);
		add(new JSeparator(), gc);

		outputLabel = new JLabel("Output");
		gc = new GridBagConstraints();
		gc.gridx = 0;
		gc.gridy = 5;
		gc.anchor = GridBagConstraints.BASELINE_LEADING;
		gc.insets = new Insets(0, 0, 5, 5);
		add(outputLabel, gc);

		outputTextField = new JTextField();
		gc = new GridBagConstraints();
		gc.gridx = 1;
		gc.gridy = 5;
		gc.fill = GridBagConstraints.HORIZONTAL;
		gc.anchor = GridBagConstraints.BASELINE_LEADING;
		gc.weightx = 10.0;
		gc.insets = new Insets(0, 0, 5, 5);
		add(outputTextField, gc);

		outputButton = new JButton("Open...");
		gc = new GridBagConstraints();
		gc.gridx = 2;
		gc.gridy = 5;
		gc.fill = GridBagConstraints.HORIZONTAL;
		gc.anchor = GridBagConstraints.BASELINE_LEADING;
		gc.insets = new Insets(0, 0, 5, 0);
		add(outputButton, gc);
		FileChooserBuilder.addHandler(outputButton, outputTextField, false);

		rfcCheckBox = new JCheckBox("Export RFC 1421 Format");
		gc = new GridBagConstraints();
		gc.gridx = 0;
		gc.gridwidth = 3;
		gc.gridy = 6;
		gc.fill = GridBagConstraints.HORIZONTAL;
		gc.anchor = GridBagConstraints.LINE_END;
		add(rfcCheckBox, gc);

		// glue
		gc = new GridBagConstraints();
		gc.gridx = 0;
		gc.gridy = 7;
		gc.gridwidth = 3;
		gc.weightx = 10.0;
		gc.weighty = 10.0;
		add(Box.createGlue(), gc);
	}
}
